/*
 * =====================================================================================
 *
 *       Filename:  ListSortLargeTest.cpp
 *
 *    Description:  链表排序算法测试
 *
 *        Version:  1.0
 *        Created:  2011-10-31 22:44:13
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (),
 *        Company:
 *
 * =====================================================================================
 */

#include "ListProblem.h"
#include <ctime>
#include <iostream>
#include <unistd.h>

#define LARGE_COUNT 10000000

int clock2milliseconds(const clock_t &start, const clock_t &finish)
{
    return (finish - start) / CLOCKS_PER_SEC * 1000;
}

void runN2Algorithms(void)
{
    clock_t start, finish;
    {
        ListNode<int> *head = createRandList<int>(LARGE_COUNT);

        start = clock();
        head = bubbleSortList(head);
        finish = clock();
        std::cout << "bubbleSortList running time is "
                  << clock2milliseconds(start, finish)
                  << " milliseconds"
                  << std::endl;
    }
    {
        ListNode<int> *head = createRandList<int>(LARGE_COUNT);

        start = clock();
        head = selectSortList(head);
        finish = clock();
        std::cout << "selectSortList running time is "
                  << clock2milliseconds(start, finish)
                  << " milliseconds"
                  << std::endl;
    }
    {
        ListNode<int> *head = createRandList<int>(LARGE_COUNT);

        start = clock();
        head = insertSortList(head);
        finish = clock();
        std::cout << "insertSortList running time is "
                  << clock2milliseconds(start, finish)
                  << " milliseconds"
                  << std::endl;
    }
}

void runNlgnAlgorithms(void)
{
    clock_t start, finish;
    {
        ListNode<int> *head = createRandList<int>(LARGE_COUNT);

        start = clock();
        head = mergeSortList(head);
        finish = clock();
        std::cout << "mergeSortList running time is "
                  << clock2milliseconds(start, finish)
                  << " milliseconds"
                  << std::endl;
    }
    {
        ListNode<int> *head = createRandList<int>(LARGE_COUNT);

        start = clock();
        head = mergeSortListBU(head);
        finish = clock();
        std::cout << "mergeSortListBU running time is "
                  << clock2milliseconds(start, finish)
                  << " milliseconds"
                  << std::endl;
    }
    {
        ListNode<int> *head = createRandList<int>(LARGE_COUNT);

        start = clock();
        head = quickSortList(head);
        finish = clock();
        std::cout << "quickSortList running time is "
                  << clock2milliseconds(start, finish)
                  << " milliseconds"
                  << std::endl;
    }
}

int main(int argc, char *argv[])
{
    enum OPT {OPT_NULL = 0, OPT_ALL, OPT_N2, OPT_NLGN} s_opt;
    static const char *optString = "abch";

    int opt = getopt( argc, argv, optString );

    switch( opt ) {
        case 'a':
            s_opt = OPT_ALL;
            break;

        case 'b':
            s_opt = OPT_N2;
            break;

        case 'c':
            s_opt = OPT_NLGN;
            break;

        case 'h':
            std::cout << "-a show all algorithms.\n"
                      << "-b show n^2 algorithms.\n"
                      << "-c show nlgn algorithms.";
            return 0;
            break;

        default:
            s_opt = OPT_ALL;
            break;
    }

    std::cout << "the max random count is " << LARGE_COUNT << std::endl;

    if (s_opt == OPT_ALL || s_opt == OPT_N2) {
        runN2Algorithms();
    }

    if (s_opt == OPT_ALL || s_opt == OPT_NLGN) {
        runNlgnAlgorithms();
    }

    return 0;
}


